/*
 * Copyright (c) 2009, Andrey Shvetsov
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <xintlibio.h>
#include <xintlib.h>
#include <iostream>
#include <sstream>
#include <iomanip>

using namespace std;

using xintlib::hex2xint;
using xintlib::xint;
using xintlib::for_each_unit;


typedef xint<32,unsigned char> xxint32;


namespace arithm {

    struct plus_op {
        const char *name() const { return "+"; }
        template <class T> T operator()(T a, T b) { return a + b; }
    };

    struct minus_op {
        const char *name() const { return "-"; }
        template <class T> T operator()(T a, T b) { return a - b; }
    };


    struct builder {
        builder(): number(0) {}
        void operator()(unsigned char u) { number = number<<8 | u; }
        int number;
    };

    template <class OP>
    static bool test_case(int a, int b, OP op)
    {
        stringstream ssa;
        ssa.fill('0');
        ssa << hex << setw(8) << a;
        xxint32 xa = hex2xint<xxint32>( ssa.str().c_str() );

        stringstream ssb;
        ssb.fill('0');
        ssb << hex << setw(8) << b;
        xxint32 xb = hex2xint<xxint32>( ssb.str().c_str() );

        xxint32 result = op( xa, xb );
        int expected = op( a, b );

        builder bld;
        for_each_unit( result, bld );

        cout << hex; cout.fill('0');
        cout << "0x" << ssa.str() << " " << op.name() << " 0x" << ssb.str() << " = 0x" << setw(8) << bld.number << ", ";

        if( bld.number != expected )
        {
            cout << "error, expected 0x" << setw(8) << expected << endl;
            return false;
        }

        cout << "ok" << endl;
        return true;
    }

    static int subtest(int a, int b)
    {
        return
            !test_case( a, b, plus_op() ) +
            !test_case( b, a, plus_op() ) +
            !test_case( a, b, minus_op() ) +
            !test_case( b, a, minus_op() );
    }

} // namespace arithm

namespace rel {

    struct lt_op {
        const char *name() const { return "<"; }
        template <class T> bool operator()(T a, T b) { return a < b; }
    };

    struct gt_op {
        const char *name() const { return ">"; }
        template <class T> bool operator()(T a, T b) { return a > b; }
    };

    struct lteq_op {
        const char *name() const { return "<="; }
        template <class T> bool operator()(T a, T b) { return a <= b; }
    };

    struct gteq_op {
        const char *name() const { return ">="; }
        template <class T> bool operator()(T a, T b) { return a >= b; }
    };

    struct eq_op {
        const char *name() const { return "=="; }
        template <class T> bool operator()(T a, T b) { return a == b; }
    };

    struct neq_op {
        const char *name() const { return "!="; }
        template <class T> bool operator()(T a, T b) { return a != b; }
    };

    template <class OP>
    static bool test_case(int a, int b, OP op)
    {
        stringstream ssa;
        ssa.fill('0');
        ssa << hex << setw(8) << a;
        xxint32 xa = hex2xint<xxint32>( ssa.str().c_str() );

        stringstream ssb;
        ssb.fill('0');
        ssb << hex << setw(8) << b;
        xxint32 xb = hex2xint<xxint32>( ssb.str().c_str() );

        bool result = op( xa, xb );
        bool expected = op( a, b );

        cout << hex; cout.fill('0');
        cout << "0x" << ssa.str() << " " << op.name() << " 0x" << ssb.str() << " = " << result << ", ";

        if( result != expected )
        {
            cout << "error, expected " << expected << endl;
            return false;
        }

        cout << "ok" << endl;
        return true;
    }

    static int subtest(int a, int b)
    {
        return
            !test_case( a, b, lt_op() ) +
            !test_case( a, b, gt_op() ) +
            !test_case( a, b, lteq_op() ) +
            !test_case( a, b, gteq_op() ) +
            !test_case( a, b, eq_op() ) +
            !test_case( a, b, neq_op() );
    }


    template <int N>
    int search_both_args(int left, const int *a) {
        return search_both_args<N-1>( left, a ) + rel::subtest( left, a[N-1] ); }

    template <>
    int search_both_args<0>(int, const int *) { return 0; }

    template <int N, int M>
    struct search {
        static int left_arg(const int *a) {
            return search<N,M-1>::left_arg( a ) + search_both_args<N>( a[M-1], a ); }
    };

    template <int N>
    struct search<N,0> {
        static int left_arg(const int *) { return 0; }
    };

    template<class T, int N>
    int all_subtests(T (&a)[N])
    {
        return search<N,N>::left_arg( a );
    }

} // namespace rel

bool unit_test()
{
    int const rel_data[] = {
        0xffffff00, 0xc1234500, 0x712345ff, 0x000000ff, 0x00000000 };

    int const errors_number =
        arithm::subtest( 0x50505050, 0x6b7c8d9e ) +
        arithm::subtest( 0x505050c0, 0x6b7c8d9e ) +
        arithm::subtest( 0x5050c050, 0x6b7c8d9e ) +
        arithm::subtest( 0x5050c0c0, 0x6b7c8d9e ) +
        arithm::subtest( 0x50c05050, 0x6b7c8d9e ) +
        arithm::subtest( 0x50c050c0, 0x6b7c8d9e ) +
        arithm::subtest( 0x50c0c050, 0x6b7c8d9e ) +
        arithm::subtest( 0x50c0c0c0, 0x6b7c8d9e ) +
        arithm::subtest( 0xc0505050, 0x6b7c8d9e ) +
        arithm::subtest( 0xc05050c0, 0x6b7c8d9e ) +
        arithm::subtest( 0xc050c050, 0x6b7c8d9e ) +
        arithm::subtest( 0xc050c0c0, 0x6b7c8d9e ) +
        arithm::subtest( 0xc0c05050, 0x6b7c8d9e ) +
        arithm::subtest( 0xc0c050c0, 0x6b7c8d9e ) +
        arithm::subtest( 0xc0c0c050, 0x6b7c8d9e ) +
        arithm::subtest( 0xc0c0c0c0, 0x6b7c8d9e ) +
        rel::all_subtests( rel_data );

    cout << dec;

    if( errors_number )
        cout << "total errors: " << errors_number << endl;
    else
        cout << "all tests are ok" << endl;

    return true;
}
